<?php

class Zy_caselist extends CI_Controller {
	function __construct()
	{
		parent::__construct();
		header("Content-type:text/html;charset=utf-8");
		
		$this->load->library('pagination');
		$this->load->helper('url');
		$this->load->helper('form');

		$this->load->database();
	}

	function index()
	{
		$dbtable = $this->load->get_var('dbtable');

		$querystring = "select * from $dbtable order by date desc, `index` desc";

		$query = $this->db->query($querystring);
		$num = $query->num_rows();

		if (!$query)
		{
			echo "查询病例失败！";
			return;
		}

		$pagesObj = $this->getPages($num);

		$per = $pagesObj->per_page;
		 
    $cur = $pagesObj->cur_page;
    $cur < 0 ? $cur = 0 : $cur;

    $querystring = "$querystring LIMIT $cur, $per";

		$query = $this->db->query($querystring);
		
		if (!$query)
		{
			echo "查询病例失败！";
			return;
		}
		
		$data['queryresult'] = $query;
		$data['table_bgcolor'] = $this->load->get_var('table_bgcolor');

		$this->load->view('Zy_view_caselist', $data);
	}

	function indexfrom()
	{
		$cur = $this->input->get('per_page');
		
		if (!$cur)
		{
			$cur = 0;
		}
		
		$dbtable = $this->load->get_var('dbtable');

		$querystring = "select * from $dbtable order by date desc, `index` desc";

		$query = $this->db->query($querystring);
		$num = $query->num_rows();

		if (!$query)
		{
			echo "查询病例失败！";
			return;
		}

		$pagesObj = $this->getPages($num);

		$per = $pagesObj->per_page;

    $querystring = "$querystring LIMIT $cur, $per";

		$query = $this->db->query($querystring);
		
		if (!$query)
		{
			echo "查询病例失败！";
			return;
		}
		
		$data['queryresult'] = $query;
		$data['table_bgcolor'] = $this->load->get_var('table_bgcolor');
		
		$this->load->view('Zy_view_caselist', $data);
	}
	
	function getPages($num)
	{
		$config['base_url'] = base_url().'index.php?c=zy_caselist&m=indexfrom';
		$config['total_rows'] = $num;
		$config['per_page'] = '10';
		$config['uri_segment'] = 3;
		$config['cur_page'] = $this->uri->segment(3, 0);
		$config['num_links'] = 3;

		$config['first_link'] = '首页';
  	$config['last_link'] = '末页';
  	$config['next_link'] = '下一页>';
  	$config['prev_link'] = '<上一页';
  	
  	$config['page_query_string'] = TRUE;

		$this->pagination->initialize($config);
		
		return $this->pagination;
	}
	
	function modify()
	{
		$dbtable = $this->load->get_var('dbtable');

		$config['uri_segment'] = 3;
		$this->pagination->initialize($config);
		
		$index = $this->uri->segment(3, 0);

		$querystring = "select * from $dbtable where $dbtable.index=$index;";

		$query = $this->db->query($querystring);

		if (!$query)
		{
			$url = base_url().'index.php/zy_caselist/index/0';
			echo "查找病例失败！<a href=$url>回到病例汇总</a>";
			return;
		}
		
		if ($query->num_rows() != 1)
		{
			$url = base_url().'index.php/zy_caselist/index/0';
			echo "查找病例失败！<a href=$url>回到病例汇总</a>";
			return;
		}

		$data['row'] = $query->row();
		
		$this->load->view('Zy_view_modifycase', $data);
	}

	function second()
	{
		$reexam_check = $this->load->get_var('reexam_check');
		$dbtable = $this->load->get_var('dbtable');

		$config['uri_segment'] = 3;
		$this->pagination->initialize($config);
		
		$index = $this->uri->segment(3, 0);

		$querystring = "select * from $dbtable where $dbtable.index=$index;";

		$query = $this->db->query($querystring);

		if (!$query)
		{
			$url = base_url().'index.php/zy_caselist/index/0';
			echo "查找病例失败！<a href=$url>回到病例汇总</a>";
			return;
		}
		
		if ($query->num_rows() != 1)
		{
			$url = base_url().'index.php/zy_caselist/index/0';
			echo "查找病例失败！<a href=$url>回到病例汇总</a>";
			return;
		}

		$data['row'] = $query->row();

		if (!strcmp($reexam_check, "off"))
		{
			$this->load->view('Zy_view_secondcase1', $data);
		}
		else
		{
			$this->load->view('Zy_view_secondcase2', $data);
		}
	}
	
	function clear_files($files)
	{
		$uploads_dir = $this->load->get_var('uploaddir')."/";

		for ($i = 0; $i < $files[$i]; $i++)
		{
			if (!strcmp($files[$i], ""))
			{
				continue;
			}
			
			unlink(iconv("utf-8","gb2312",$uploads_dir.$files[$i]));
		}
	}

	function rename_files($name, $date, &$files)
	{
		$uploads_dir = $this->load->get_var('uploaddir')."/";

		for ($i = 0; $i < count($files); $i++)
		{
			if (!strcmp($files[$i], ""))
			{
				continue;
			}

			$newfile = $name."_".$date."_".$files[$i];

			if (rename($uploads_dir.$files[$i], iconv("utf-8","gb2312",$uploads_dir.$newfile)))
			{
				$files[$i] = $newfile;
			}
		}
	}
	
	function upload(&$files)
	{
		$uploads_dir = $this->load->get_var('uploaddir')."/";

		if (!file_exists ($uploads_dir))
		{
			if (!mkdir($uploads_dir))
			{
				return -1;
			}
		}

		$config['upload_path'] = $uploads_dir;
		$config['allowed_types'] = 'gif|jpg|png|bmp|pdf|rar|zip';
		$config['max_size'] = '30720';
		$config['remove_spaces'] = TRUE;
		
		$config['file_name'] = time();
		$this->load->library('upload', $config);

		for ($i = 0; $i < 5; $i++)
		{
			$files[$i] = "";

			$fieldname = "patient_file".($i + 1);
			$fieldvalue = $_FILES[$fieldname]['name'];

			if (empty($fieldvalue))
			{
				continue;
			}

			if(!$this->upload->do_upload($fieldname))
			{
				echo $this->upload->display_errors();
				$this->clear_files($files);
				return -2;
			}
	
			$updata = $this->upload->data();
			$img = $updata['file_name'];

			$files[$i] = $img;
		}

		return 0;
	}

	function modifydone()
	{
		$dbtable = $this->load->get_var('dbtable');

		$config['uri_segment'] = 3;
		$this->pagination->initialize($config);
		
		$index = $this->uri->segment(3, 0);
		
		$oldfiles_str = "";

		{
			$querystring = "select * from $dbtable where $dbtable.index=$index;";
	
			$query = $this->db->query($querystring);
	
			if (!$query)
			{
				$url = base_url().'index.php/zy_caselist/index/0';
				echo "修改病例失败！<a href=$url>回到病例汇总</a>";
				return;
			}
	
			if ($query->num_rows() != 1)
			{
				$url = base_url().'index.php/zy_caselist/index/0';
				echo "查找病例失败！<a href=$url>回到病例汇总</a>";
				return;
			}
	
			$row = $query->row();
			
			$oldfiles_str = $row->files;
		}
		
		$patient_name=$this->input->post('patient_name');
		$patient_name = Myphplib::trim_space($patient_name);
		
		if (!strcmp($patient_name, ""))
		{
			echo "请输入姓名!";
			return;	
		}
		
		$patient_sex=$this->input->post('patient_sex');
		
		if (!strcmp($patient_sex, ""))
		{
			echo "请输入性别!";
			return;	
		}

		$patient_age=$this->input->post('patient_age');	
		$ageunit=$this->input->post('ageunit');
		
		if (!strcmp($ageunit, 'M'))
		{
			$patient_age -= 1000;
		}
		 
		$patient_date=$this->input->post('patient_date');	
		
		if (!strcmp($patient_date, ""))
		{
			echo "请输入日期!";
			return;	
		}

		$patient_attackdate=$this->input->post('patient_attackdate');	

		$patient_phonenum=$this->input->post('patient_phonenum');	
		$patient_phonenum = Myphplib::trim_space($patient_phonenum);
		
		$patient_addr=$this->input->post('patient_addr');	

		$patient_diag=$this->input->post('patient_diag');

		if (!strcmp($patient_diag, ""))
		{
			echo "请输入病史!";
			return;	
		}
		
		$patient_prescribe=$this->input->post('patient_prescribe');
		$patient_usemethod=$this->input->post('patient_usemethod');
		$patient_comment=$this->input->post('patient_comment');

		$this->load->database();

		$files = array();

		if ($this->upload($files) < 0)
		{
			echo "上传文件失败!";
			return;
		}

		$this->rename_files($patient_name, $patient_date, $files);

		$files_str = "";

		for ($i = 0; $i < count($files); $i++)
		{
			if (!strcmp($files[$i], ""))
			{
				continue;
			}

			$files_str .= $files[$i].";";
		}

		$data = array(
			'name' => $patient_name, 'sex' => $patient_sex, 'age' => $patient_age, 'date' => $patient_date,
			'diag' => $patient_diag,
			'prescribe' => $patient_prescribe,
			'usemethod' => $patient_usemethod,
			'comment' => $patient_comment,
			'phonenum' => $patient_phonenum,
			'addr' => $patient_addr,
			'files' => $oldfiles_str.$files_str,
			'attackdate' => $patient_attackdate,
			);

		$querystring = $this->db->update_string($dbtable, $data, "$dbtable.index=$index");
		
		if (!$this->db->query($querystring))
		{
			$url = base_url().'index.php/zy_caselist/index/0';
			echo "修改病例失败！<a href=$url>回到病例汇总</a>";
			return;
		}

		$prescribename=$this->input->post('prescribename');
		$patient_newprescribe=$this->input->post('patient_newprescribe');	
		
		if ($patient_newprescribe && !strcmp($patient_newprescribe, "on"))
		{
			$this->newprescribe($prescribename, $patient_prescribe, $patient_usemethod);
		}
			
		$this->load->view('Zy_view_modifycasesuccess');
	}

	function newprescribe($prescribe_name, $prescribe_value, $prescribe_usemethod)
	{
		if (!$prescribe_name || !$prescribe_value)
		{
			return;	
		}

		$prescribetable = $this->load->get_var('prescribetable');

		$prescribe_name = Myphplib::trim_space($prescribe_name);

		if (!strcmp($prescribe_name, ""))
		{
			return;	
		}

		if (!strcmp($prescribe_value, ""))
		{
			return;	
		}

		$prescribe_namestr = $this->db->escape_str($prescribe_name);
		
		$querystring = "select * from $prescribetable where `name`='$prescribe_namestr'";
		$query = $this->db->query($querystring);
		
		if (!$query)
		{
			return;
		}

		if ($query->num_rows() != 0)
		{
			return;
		}

		$data = array(
			'name' => $prescribe_name,
			'value' => $prescribe_value,
			'usemethod' => $prescribe_usemethod,
			);

		$querystring = $this->db->insert_string($prescribetable, $data);
		
		if (!$this->db->query($querystring))
		{
			return;
		}
	}
	
	function secondcase2done()
	{
		$dbtable = $this->load->get_var('dbtable');

		$config['uri_segment'] = 3;
		$this->pagination->initialize($config);
		
		$index = $this->uri->segment(3, 0);
		
		$oldfiles_str = "";
		$oldrow = array();

		{
			$querystring = "select * from $dbtable where $dbtable.index=$index;";
	
			$query = $this->db->query($querystring);
	
			if (!$query)
			{
				$url = base_url().'index.php/zy_caselist/index/0';
				echo "修改病例失败！<a href=$url>回到病例汇总</a>";
				return;
			}
	
			if ($query->num_rows() != 1)
			{
				$url = base_url().'index.php/zy_caselist/index/0';
				echo "查找病例失败！<a href=$url>回到病例汇总</a>";
				return;
			}
	
			$oldrow = $query->row();
			
			$oldfiles_str = $oldrow->files;
		}

		$patient_diag=$this->input->post('patient_diag');

		if (!strcmp($patient_diag, ""))
		{
			echo "请输入病史!";
			return;	
		}
		
		$patient_prescribe=$this->input->post('patient_prescribe');
		$patient_usemethod=$this->input->post('patient_usemethod');
		$patient_comment=$this->input->post('patient_comment');

		$this->load->database();

		$files = array();

		if ($this->upload($files) < 0)
		{
			echo "上传文件失败!";
			return;
		}

		$this->rename_files($oldrow->name, $oldrow->date, $files);

		$files_str = "";

		for ($i = 0; $i < count($files); $i++)
		{
			if (!strcmp($files[$i], ""))
			{
				continue;
			}

			$files_str .= $files[$i].";";
		}

		$data = array(
			'diag' => $oldrow->diag."\n".$patient_diag,
			'prescribe' => $oldrow->prescribe."\n".$patient_prescribe,
			'usemethod' => $oldrow->usemethod."\n".$patient_usemethod,
			'comment' => $oldrow->comment."\n".$patient_comment,
			'files' => $oldfiles_str.$files_str,
			);

		$querystring = $this->db->update_string($dbtable, $data, "$dbtable.index=$index");
		
		if (!$this->db->query($querystring))
		{
			$url = base_url().'index.php/zy_caselist/index/0';
			echo "复诊病例失败！<a href=$url>回到病例汇总</a>";
			return;
		}

		$prescribename=$this->input->post('prescribename');	
		$patient_newprescribe=$this->input->post('patient_newprescribe');	
		
		if ($patient_newprescribe && !strcmp($patient_newprescribe, "on"))
		{
			$this->newprescribe($prescribename, $patient_prescribe, $patient_usemethod);
		}
			
		$data['title'] = '复诊成功！';
		$data['linkstr'] = 'index.php/zy_caselist/index/0';
		$data['message'] = '回到病例汇总';
		
		$this->load->view('Zy_view_operationresult', $data);
	}
	
	function delete()
	{
		$dbtable = $this->load->get_var('dbtable');

		$config['uri_segment'] = 3;
		$this->pagination->initialize($config);
		
		$index = $this->uri->segment(3, 0);

		$querystring = "select * from $dbtable where $dbtable.index=$index;";

		$query = $this->db->query($querystring);

		if (!$query)
		{
			$url = base_url().'index.php/zy_caselist/index/0';
			echo "删除病例失败！<a href=$url>回到病例汇总</a>";
			return;
		}

		if ($query->num_rows() != 1)
		{
			$url = base_url().'index.php/zy_caselist/index/0';
			echo "查找病例失败！<a href=$url>回到病例汇总</a>";
			return;
		}

		$row = $query->row();

  	$files = explode(";", $row->files);

  	for ($i = 0; $i < count($files); $i++)
  	{
  		if (!strcmp($files[$i], ""))
  		{
  			continue;	
  		}
  		
  		$filepath = $this->load->get_var('uploaddir')."/$files[$i]";
			unlink(iconv("utf-8","gb2312",$filepath));
  	}
  	
  	//$this->clear_files($files);

		$querystring = "delete from $dbtable where $dbtable.index=$index;";

		$query = $this->db->query($querystring);

		if (!$query)
		{
			$url = base_url().'index.php/zy_caselist/index/0';
			echo "删除病例失败！<a href=$url>回到病例汇总</a>";
			return;
		}

		$this->load->view('Zy_view_deletecasesuccess');
	}

	function deletefile()
	{
		$id = $this->input->get('id');
		$file = $this->input->get('file');

		$dbtable = $this->load->get_var('dbtable');

		$querystring = "select * from $dbtable where $dbtable.index=$id;";

		$query = $this->db->query($querystring);

		if (!$query)
		{
			$url = base_url().'index.php/zy_caselist/index/0';
			echo "删除病例文件失败！<a href=$url>回到病例汇总</a>";
			return;
		}

		if ($query->num_rows() != 1)
		{
			$url = base_url().'index.php/zy_caselist/index/0';
			echo "查找病例失败！<a href=$url>回到病例汇总</a>";
			return;
		}

		$row = $query->row();

		$filepath = "";

  	$files = explode(";", $row->files);

  	for ($i = 0; $i < count($files); $i++)
  	{
  		if (strcmp($files[$i], $file))
  		{
  			continue;	
  		}
  		
			array_splice($files, $i, 1);
			$filepath = $this->load->get_var('uploaddir')."/$file";
			
			break;
  	}

  	if (!strcmp($filepath, ""))
  	{
			$url = base_url().'index.php/zy_caselist/index/0';
			echo "删除病例文件失败！<a href=$url>回到病例汇总</a>";
			return;
  	}

		$files_str = "";

		for ($i = 0; $i < count($files); $i++)
		{
			if (!strcmp($files[$i], ""))
			{
				continue;
			}

			$files_str .= $files[$i].";";
		}

		$data = array(
			'files' => $files_str,
			);

		$querystring = $this->db->update_string($dbtable, $data, "$dbtable.index=$id");

		$query = $this->db->query($querystring);

		if (!$query)
		{
			$url = base_url().'index.php/zy_caselist/index/0';
			echo "删除病例文件失败！<a href=$url>回到病例汇总</a>";
			return;
		}
		
		if (unlink(iconv("utf-8","gb2312",$filepath)))
		{

		}

		$newurl = base_url()."index.php/zy_caselist/modify/".$id;

		header("Location: $newurl");
	}
	
	function printcase()
	{
		$dbtable = $this->load->get_var('dbtable');
		$only_prtprescribe = $this->load->get_var('only_prtprescribe');

		$config['uri_segment'] = 3;
		$this->pagination->initialize($config);
		
		$index = $this->uri->segment(3, 0);

		$querystring = "select * from $dbtable where $dbtable.index=$index;";

		$query = $this->db->query($querystring);

		if (!$query)
		{
			$url = base_url().'index.php/zy_caselist/index/0';
			echo "查找病例失败！<a href=$url>回到病例汇总</a>";
			return;
		}
		
		if ($query->num_rows() != 1)
		{
			$url = base_url().'index.php/zy_caselist/index/0';
			echo "查找病例失败！<a href=$url>回到病例汇总</a>";
			return;
		}

		$data['row'] = $query->row();

		if (!strcmp($only_prtprescribe, "off"))
		{
			$this->load->view('Zy_view_printcase', $data);
		}
		else
		{
			$this->load->view('Zy_view_printprescribe', $data);
		}
	}
	
	function showpictures()
	{
		$dbtable = $this->load->get_var('dbtable');

		$config['uri_segment'] = 3;
		$this->pagination->initialize($config);
		
		$index = $this->uri->segment(3, 0);

		$querystring = "select * from $dbtable where $dbtable.index=$index;";

		$query = $this->db->query($querystring);

		if (!$query)
		{
			$url = base_url().'index.php/zy_caselist/index/0';
			echo "查找病例失败！<a href=$url>回到病例汇总</a>";
			return;
		}
		
		if ($query->num_rows() != 1)
		{
			$url = base_url().'index.php/zy_caselist/index/0';
			echo "查找病例失败！<a href=$url>回到病例汇总</a>";
			return;
		}

		$data['row'] = $query->row();
		
		$this->load->view('Zy_view_pictures', $data);
	}
}

?>